package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.FragmentStareAdapter;
import com.tianxin.workbao.workBao.bean.Organization;
import com.tianxin.workbao.workBao.dataProvide.OrganizationDataProvide;
import com.tianxin.workbao.workBao.fragment.StaffContactFragment;
import com.tianxin.workbao.workBao.holder.SearchViewHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 员工通信录
 * Created by hacker on 2017/6/17.
 */

public class StaffContactActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager mViewPage;
    private TabLayout mTabLayout;
    private FloatingActionButton topFab;
    private List<Organization> mOrganizations = new ArrayList<>();

    private OrganizationDataProvide mOrganizationDataProvide;
    private FragmentStareAdapter staffContactAdapter;
    private TextView toolbarTitle;
    private SearchViewHolder.OnSearchHandlerListener searchHandlerListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_main_tablayout);
        super.initStatusBar(false);
        initView();
        initEvent();
        bindView();
        initData();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle(" ");
        mToolbar.setNavigationIcon(R.drawable.ic_action_back_24dp);
        setSupportActionBar(mToolbar);
        toolbarTitle = findViewById(R.id.toolbar_title);
        toolbarTitle.setText(getResources().getString(R.string.staff_content));
        mViewPage = findViewById(R.id.viewPager);
        mTabLayout = findViewById(R.id.tabLayout);
        int count = mTabLayout.getTabCount();
        for (int i = 0; i < count; i++) {
            View view = mTabLayout.getChildAt(i);
            final TextView tv = view.findViewById(android.R.id.title);
            tv.setTextSize(16);
        }
        topFab = findViewById(R.id.top_fab);
    }

    @Override
    public void initData() {
        mOrganizationDataProvide = new OrganizationDataProvide(getApplicationContext(), new UpdateListViewListener() {
            @Override
            public void onRefreshData(Object result) {
                if (!mTabTitles.isEmpty()) {
                    mTabTitles.clear();
                }
                if (!mFragments.isEmpty()) {
                    mFragments.clear();
                }
                if (!mOrganizations.isEmpty()) {
                    mOrganizations.clear();
                }
                if(result instanceof  List){
                    mOrganizations = (List<Organization>) result;
                }
                for (int i = 0; i < mOrganizations.size(); i++) {
                    mTabTitles.add(i, mOrganizations.get(i).getOgname());
                    mFragments.add(StaffContactFragment.newInstance(Integer.valueOf(mOrganizations.get(i).getId() + "")));
                }
                /**
                 * update view
                 */
                staffContactAdapter.notifyDataSetChanged();
            }

            @Override
            public void onLoadMoreData(Object result) {

            }

            @Override
            public void showMessage(String msg) {
                Toast.makeText(StaffContactActivity.this, msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void showProgress() {
                getSweetAlertDialog(StaffContactActivity.this).showDialog("", 1200);
            }

            @Override
            public void hideProgress() {
                getSweetAlertDialog(StaffContactActivity.this).dismiss();
            }
        });
        mOrganizationDataProvide.getOrganizations();
    }

    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StaffContactActivity.this.finish();
            }
        });
        topFab.setOnClickListener(this);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                searchTag = tab.getPosition();
                searchHint = tab.getText().toString();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void bindView() {
        staffContactAdapter = new FragmentStareAdapter(getSupportFragmentManager(), mFragments, mTabTitles);
        mViewPage.setAdapter(staffContactAdapter);
        mViewPage.setOffscreenPageLimit(3);
        mViewPage.setCurrentItem(0);
        mTabLayout.setupWithViewPager(mViewPage);
        mTabLayout.setSelectedTabIndicatorColor(this.getResources().getColor(R.color.white));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.search:
                // 显示 搜索框
                showSearchView(this, getSearchHandlerListener());
                return true;
        }
        return super.onOptionsItemSelected(item);
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_fab:
                updateUi();
                break;
        }
    }

    private void updateUi() {
        mFragments.get(searchTag).initData(false);
    }

}
